//
//  HomePageSelecCell.swift
//  HappyEat
//
//  Created by ctx2016 on 17/1/8.
//  Copyright © 2017年 陌路是我. All rights reserved.
//

import UIKit

class HomePageSelecCell: UITableViewCell {
    let selectView:UIView = UIView.init()
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }

}
extension HomePageSelecCell{
    func setUI(){
        let titleNames:[String] = ["人气","进度"]
        for i in 0 ..< titleNames.count {
            let titleLabel:UILabel = UILabel.init()
            self.contentView.addSubview(titleLabel)
            self.contentView.addSubview(selectView)
            titleLabel.snp.makeConstraints({ (make) in
                make.left.equalTo((k_Wide/2 - 55.0)/2 + (CGFloat(i)*(k_Wide/2)))
                make.size.equalTo(CGSize.init(width: 55, height: 16))
                make.top.equalTo(12)
            })
            selectView.snp.makeConstraints({ (make) in
                make.left.equalTo((k_Wide/2 - 55.0)/2 + (CGFloat(0) * (k_Wide/2)))
                make.size.equalTo(CGSize.init(width: 55, height: 2))
                make.bottom.equalTo(0)
            })
            titleLabel.textAlignment = .center
            selectView.backgroundColor = .red
            titleLabel.text = titleNames[i]
            let conTrol:UIControl = UIControl.init()
            self.contentView.addSubview(conTrol)
            conTrol.snp.makeConstraints({ (make) in
                make.left.equalTo((CGFloat(i)*(k_Wide/2)))
                make.size.equalTo(CGSize.init(width: (k_Wide/2), height: 40))
                make.top.equalTo(0)
            })
            conTrol.tag = 1000+i
            conTrol.addTarget(self, action: #selector(selectedAction(_:)), for: .touchUpInside)
        }
    }
    func selectedAction(_ control:UIControl){
        
        UIView.animate(withDuration: 0.3) {
            self.selectView.transform.translatedBy(x: (k_Wide/2 - 55.0)/2 + (CGFloat(0) * (k_Wide/2)), y: 0)
            self.selectView.snp.removeConstraints()
            self.selectView.snp.makeConstraints({ (make) in
                make.left.equalTo((k_Wide/2 - 55.0)/2 + (CGFloat(control.tag - 1000) * (k_Wide/2)))
                make.size.equalTo(CGSize.init(width: 55, height: 2))
                make.bottom.equalTo(0)
            })
        }
        switch control.tag{
        case 1000:
            print("renqi")
        case 1001:
            print("进度")
        default:
            print("哈哈")
        }
    }
}
